package logic

import (
	"context"
	"friend/common"

	"friend/internal/svc"
	"friend/pb/friend"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateRelationLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateRelationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateRelationLogic {
	return &UpdateRelationLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// UpdateRelation 好友关系维护(拉黑/取消拉黑/删除)
func (l *UpdateRelationLogic) UpdateRelation(in *friend.RelationInfo) (_ *friend.Empty, err error) {
	switch in.Status {
	case common.FriendRelationStatusNormal:
		err = l.svcCtx.FriendRelationExtendModel.UpdateStatus(
			l.ctx, in.UserId, in.FriendId, common.FriendRelationStatusNormal, common.FriendRelationStatusNormal,
		)
	case common.FriendRelationStatusBlack:
		err = l.svcCtx.FriendRelationExtendModel.UpdateStatus(
			l.ctx, in.UserId, in.FriendId, common.FriendRelationStatusBlack, common.FriendRelationStatusBlacked,
		)
	case common.FriendRelationStatusDelete:
		err = l.svcCtx.FriendRelationExtendModel.UpdateStatus(
			l.ctx, in.UserId, in.FriendId, common.FriendRelationStatusDelete, common.FriendRelationStatusDeleted,
		)
	}
	if err != nil {
		return nil, err
	}
	return &friend.Empty{}, nil
}
